package com.alander.nexusgo.post.service.impl;

import com.alander.nexusgo.common.model.dto.PostCommentsListDTO;
import com.alander.nexusgo.common.model.vo.PostCommentsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alander.nexusgo.post.model.entity.PostComments;
import com.alander.nexusgo.post.service.PostCommentsService;
import com.alander.nexusgo.post.mapper.PostCommentsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author alander
*/
@Service
public class PostCommentsServiceImpl extends ServiceImpl<PostCommentsMapper, PostComments>
    implements PostCommentsService{
    
    @Override
    public PostCommentsVO createComment(PostComments comment, Long userId) {
        comment.setUserId(userId);
        this.save(comment);
        return convertToVO(comment);
    }
    
    @Override
    public PostCommentsVO updateComment(Long id, PostComments comment, Long userId) {
        PostComments existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("评论不存在");
        }
        
        if (!existing.getUserId().equals(userId)) {
            throw new RuntimeException("无权限修改他人评论");
        }
        
        comment.setId(id);
        this.updateById(comment);
        return convertToVO(this.getById(id));
    }
    
    @Override
    public boolean deleteComment(Long id, Long userId) {
        PostComments existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("评论不存在");
        }
        
        if (!existing.getUserId().equals(userId)) {
            throw new RuntimeException("无权限删除他人评论");
        }
        
        // 软删除
        LambdaUpdateWrapper<PostComments> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PostComments::getId, id)
                .set(PostComments::getDelFlag, "1");
        return this.update(updateWrapper);
    }
    
    @Override
    public PostCommentsVO getCommentById(Long id) {
        PostComments comment = this.getById(id);
        return comment != null ? convertToVO(comment) : null;
    }
    
    @Override
    public List<PostCommentsVO> listComments(PostCommentsListDTO postCommentsListDTO) {
        LambdaQueryWrapper<PostComments> queryWrapper = new LambdaQueryWrapper<>();
        
        if (postCommentsListDTO.getPostId() != null) {
            queryWrapper.eq(PostComments::getPostId, postCommentsListDTO.getPostId());
        }
        
        if (postCommentsListDTO.getUserId() != null) {
            queryWrapper.eq(PostComments::getUserId, postCommentsListDTO.getUserId());
        }
        
        if (postCommentsListDTO.getParentId() != null) {
            queryWrapper.eq(PostComments::getParentId, postCommentsListDTO.getParentId());
        }
        
        if (postCommentsListDTO.getStatus() != null) {
            queryWrapper.eq(PostComments::getStatus, postCommentsListDTO.getStatus());
        }
        
        // 只查询未删除的评论
        queryWrapper.eq(PostComments::getDelFlag, "0");
        
        queryWrapper.orderByDesc(PostComments::getCreateTime);
        List<PostComments> commentsList = this.list(queryWrapper);
        
        return commentsList.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将PostComments实体转换为PostCommentsVO
     * @param comment PostComments实体
     * @return PostCommentsVO对象
     */
    private PostCommentsVO convertToVO(PostComments comment) {
        PostCommentsVO commentVO = new PostCommentsVO();
        BeanUtils.copyProperties(comment, commentVO);
        if (comment.getCreateTime() != null) {
            commentVO.setCreateTime(comment.getCreateTime().toString());
        }
        if (comment.getUpdateTime() != null) {
            commentVO.setUpdateTime(comment.getUpdateTime().toString());
        }
        return commentVO;
    }
}